package de.bazinga.rotw.menu;

import java.util.Iterator;
import java.util.concurrent.Callable;

import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.TextureState;
import com.jme.util.GameTaskQueueManager;
import com.jme.util.TextureManager;
import com.jmex.game.state.GameStateManager;

import de.bazinga.rotw.game.Game;
import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.map.MapList;
import de.bazinga.rotw.menu.item.MenuItem;
import de.bazinga.rotw.menu.item.TextItem;

public class ServerMenu extends Menu {

	private ColorRGBA[] colorList = {/*ColorRGBA.red,ColorRGBA.brown,ColorRGBA.blue,*/ColorRGBA.green,ColorRGBA.white};
	private String[] colorNames = {/*"red.png","brown.png","blue.png",*/"green.png","white.png"};
	private String[] playerTexture = {/*"red","brown","blue",*/"green","white"};
	private int currColorIndex = 0;
	
	private int currLevel = 0;
	
	public ServerMenu() {
		super("serverMenu");
		
		MapList.getInstance().getMapList();
	}
	
	
	protected void init() {		
		// Background
		Quad bgQuad = new Quad("bg",
				Game.getGame().getDisplay().getWidth(), 
				Game.getGame().getDisplay().getHeight());
		
		bgQuad.setRenderQueueMode(Renderer.QUEUE_ORTHO);   
	 	
		bgQuad.setLocalTranslation(new Vector3f(
				Game.getGame().getDisplay().getWidth()/2,
				Game.getGame().getDisplay().getHeight()/2,
				0));
		
		TextureState bg = Game.getGame().getDisplay().getRenderer().createTextureState();
		bg.setEnabled(true);
		bg.setTexture(TextureManager.loadTexture(RotwGameState.class
				.getClassLoader().getResource("background/menuBg.png"),
				Texture.MinificationFilter.BilinearNearestMipMap,
				Texture.MagnificationFilter.Bilinear));
		
		bgQuad.setRenderState(bg);
		
		bgQuad.setLightCombineMode(Spatial.LightCombineMode.Off);
		bgQuad.updateRenderState();
		
		getRootNode().attachChild(bgQuad);
	}
	
	
	protected void initMenuItems() {
		otherItems.put("colorString", 
				new MenuItem("color.png",
						"",
						2f,
						150,
						Game.getGame().getDisplay().getHeight() - 150,
						250,100));

		menuItems.put("color", 
				new MenuItem("color/green.png",
						"color",
						2f,
						350,
						Game.getGame().getDisplay().getHeight() - 150,
						64,64));
		
		menuItems.put("newGame", 
				new MenuItem("newGame.png",
						"newGame",
						1f,
						150,
						Game.getGame().getDisplay().getHeight() - 250,
						250,100));
		
		

		menuItems.put("serverNameButton", 
				new MenuItem("servername.png",
						"serverNameButton",
						2f,
						150,
						Game.getGame().getDisplay().getHeight()-400,
						250,100));

		textItems.put("serverName", 
				new TextItem("",
						"serverName", 
						ColorRGBA.yellow,
						ColorRGBA.black,
						2f, 
						320,
						Game.getGame().getDisplay().getHeight() - 410));
		textItems.get("serverName").setText("Unnamed");
		
		
		menuItems.put("back", 
				new MenuItem("back.png",
						"back",
						2f,
						150,
						Game.getGame().getDisplay().getHeight()-500,
						250,100));
		

		
		menuItems.put("prevLevel", 
				new MenuItem("previous.png",
						"prevLevel",
						0.08f,
						450,
						Game.getGame().getDisplay().getHeight()-270,
						64,64));
		
		otherItems.put("level", 
				new MenuItem(MapList.getInstance().getMapList().get(currLevel).get("name"),
						"level",
						ColorRGBA.black,
						2f,
						400,
						Game.getGame().getDisplay().getHeight()-220));
		
		otherItems.put("levelMode", 
				new MenuItem("("+MapList.getInstance().getMapList().get(currLevel).get("gameMode")+")",
						"levelMode",
						ColorRGBA.black,
						0.7f,
						400,
						Game.getGame().getDisplay().getHeight()-230));
		
		
		menuItems.put("nextLevel", 
				new MenuItem("next.png",
						"nextLevel",
						0.08f,
						550,
						Game.getGame().getDisplay().getHeight()-270,
						64,64));
		
		
		
	}
	
	protected void handleClick(String item) {
		if(menuItems.containsKey(item)) {
			if(item.equals("back")) {
				GameStateManager.getInstance().getChild("gameMenu").setActive(true);
				this.setActive(false);
			}else if(item.equals("newGame")) {
				//as soon as i disconnect the server-player doesnt get the right position
				((RotwGameState)GameStateManager.getInstance().getChild("gameState")).disconnect();
				
				// Set Servername
				((RotwGameState)GameStateManager.getInstance().getChild("gameState"))
					.setServerName(textItems.get("serverName").getTextOutput());
				
				// Set Bullet-Color
				((RotwGameState)GameStateManager.getInstance().getChild("gameState")).
					setPlayerBulletColor(colorList[currColorIndex]);

				// Set Player-Color
				((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getPlayer()
					.setTextureColor(playerTexture[currColorIndex]);
				
				// Set Player-Name
				((RotwGameState)GameStateManager.getInstance().getChild("gameState"))
					.getPlayer().setPlayerName(
						((RotwGameState)GameStateManager.getInstance().getChild("gameState")).getProps().getProperty("playerName"));
				
				
				GameStateManager.getInstance().getChild("serverMenu").setActive(false);
				GameStateManager.getInstance().getChild("loadingMenu").setActive(true);
				
				GameTaskQueueManager.getManager().update(new Callable<Void>(){

					public Void call() throws Exception {
				
						((LoadingState)GameStateManager.getInstance().getChild("loadingMenu")).startServer(currLevel);
						return null;
					}
				});
				
			}else if(item.equals("color")) {
				
				if(currColorIndex == colorList.length-1) {
					// Last Element, so take first
					currColorIndex = 0;
				}else{
					// NExt Element
					currColorIndex++;
				}
				menuItems.get(item).setText("color/"+colorNames[currColorIndex]);
				//menuItems.get(item).setDefaultColor(colorList[currColorIndex]);
				
			}else if(item.equals("prevLevel")) {
				if(currLevel != 0) {
					currLevel--;
					otherItems.get("level").setText(
							MapList.getInstance().getMapList().get(currLevel).get("name"));
					otherItems.get("levelMode").setText(
							"("+MapList.getInstance().getMapList().get(currLevel).get("gameMode")+")");
				}
			}else if(item.equals("nextLevel")) {
				if(currLevel <  MapList.getInstance().getMapList().size()-1) {
					currLevel++;
					otherItems.get("level").setText(
							MapList.getInstance().getMapList().get(currLevel).get("name"));
					otherItems.get("levelMode").setText(
							"("+MapList.getInstance().getMapList().get(currLevel).get("gameMode")+")");
				}
			}else if(item.equals("serverNameButton")) {
				textItems.get("serverName").setActive(true);
			}
		}
	}
	
	protected void handleHover(MenuItem hoverItem) {
		Iterator<MenuItem> it = menuItems.values().iterator();
		while(it.hasNext()) {
			MenuItem item = it.next();
			if(hoverItem == item) {
				
				if(item == menuItems.get("prevLevel")) {
					item.setText("previousRed.png");
				}else if(item == menuItems.get("nextLevel")) {
					item.setText("nextRed.png");
				}else if(item == menuItems.get("newGame")) {
					item.setText("newGameHover.png");
				}else if(item == menuItems.get("continue")) {
					item.setText("continueHover.png");
				}else if(item == menuItems.get("back")) {
					item.setText("backHover.png");
				}else if(item == menuItems.get("serverNameButton")) {
					item.setText("servernameHover.png");
				}else{
					item.setTextColor(ColorRGBA.yellow);
				}
			}else{
				if(item == menuItems.get("prevLevel")) {
					item.setText("previous.png");
				}else if(item == menuItems.get("nextLevel")) {
					item.setText("next.png");
				}else if(item == menuItems.get("newGame")) {
					item.setText("newGame.png");
				}else if(item == menuItems.get("continue")) {
					item.setText("continue.png");
				}else if(item == menuItems.get("back")) {
					item.setText("back.png");
				}else if(item == menuItems.get("serverNameButton")) {
					item.setText("servername.png");
				}else{
					item.setTextColor(item.getTextColor());
				}
			}
		}
	}
}